package muslim30;

import java.util.List;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginProcServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8377085276311844400L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws IOException {
		
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws IOException {
		
		HttpSession session = request.getSession();
		
		String memcode = new String((request.getParameter("id")).getBytes("ISO-8859-1"), "EUC-KR");
		String pass = new String((request.getParameter("apwd")).getBytes("ISO-8859-1"), "EUC-KR");
		String nationCode = new String((request.getParameter("code")).getBytes("ISO-8859-1"), "EUC-KR");
		
		memcode = memcode != null ? memcode : "";
		pass = pass != null ? pass : "";
		nationCode = nationCode != null ? nationCode : "1";
		
		//String error = "";
		
		PrmDBMgr mDB = new PrmDBMgr();
		
		List<Hashtable<String, String>> list = null;
		try {
			list = mDB.loginCheck(memcode, pass);
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		RSACryptor rsa = new RSACryptor();
		
		if(list != null && list.size() > 0) {
			Hashtable<String, String> h = list.get(0);
			//String authchk = (String)h.get("authchk");
			
			String pass2 = "";
			if(h.get("pass") != null)
				try {
					pass2=rsa.decrypt((String)h.get("pass"));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			//String ssn = (String)h.get("ssn");
			//String chaptercodeo = (String)h.get("chaptercodeo");
			String name = (String)h.get("name");
			String yn = (String)h.get("yn");
			String hp1 = (String)h.get("hp1");
			String hp2 = (String)h.get("hp2");
			String hp3 = (String)h.get("hp3");
			String hp=hp1+"-"+hp2+"-"+hp3;
			String email1 = (String)h.get("email1");
			String email2 = (String)h.get("email2");
	        String email=email1+"@"+email2;
			String chaptercode = (String)h.get("chaptercode");
			String approve = (String)h.get("approve");
			
			if(pass2.equals(pass)){
				
				session.setAttribute("Member","yes");//íì ì¸ì§ ìëì§..
                session.setAttribute("memcode",memcode);
				session.setAttribute("uid",memcode);//íììì´ë
                session.setAttribute("name",name);
				session.setAttribute("hp",hp);
				session.setAttribute("email",email);
				session.setAttribute("chaptercode",chaptercode);
				session.setAttribute("approve",approve);
				session.setAttribute("yn",yn);//íì ì´ë¦
				session.setAttribute("nation",nationCode);//íì ì´ë¦
				
				try {
					mDB.insertLogHistory(memcode,"Y",request.getRemoteAddr()  );
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				RequestDispatcher rd =
						  request.getRequestDispatcher("WEB-INF/nation.jsp?code="+nationCode);
				try {
					rd.forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				try {
					mDB.insertLogHistory(memcode,"N",request.getRemoteAddr()  );
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				response.setCharacterEncoding("EUC-KR");
				response.getWriter().print("<script>alert(\"비밀번호가 일치하지 않습니다.\");</script>");
			}
		} else {
			response.setCharacterEncoding("EUC-KR");
			response.getWriter().print("<script>alert(\"아이디를 찾을 수 없습니다.\");</script>");
		}
	}
}
